package com.kuwou.workobject.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.kuwou.workobject.Utils.QueryUtil;
import com.kuwou.workobject.commonality.Log;
import com.kuwou.workobject.pojo.entity.Channel;
import com.kuwou.workobject.pojo.entity.Music;
import com.kuwou.workobject.mapper.MusicMapper;
import com.kuwou.workobject.pojo.entity.RankList;
import com.kuwou.workobject.pojo.vo.MusicPageVo;
import com.kuwou.workobject.pojo.vo.MusicVo;
import com.kuwou.workobject.service.MusicService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;


@RequiredArgsConstructor
@Service
public class MusicServiceImpl extends ServiceImpl<MusicMapper, Music> implements MusicService {


    @Lazy
    private final  MusicMapper musicMapper;


    private Logger logger = null;

    {
        //获取日志对象
        logger = Log.getLogger(MusicServiceImpl.class);
    }


    //清空表
    @Override
    public void truncateTable() {
        musicMapper.truncateTable();
    }

    //查询歌曲信息
    @Override
    public MusicPageVo queryMusicInfo(String mes, String type, int pageNo) throws IOException {

        List<RankList> rankLists = QueryUtil.getAllRankList();
        List<Channel> channels = QueryUtil.getAllChannel();

        Map<Integer, String> channelMap = channels.stream().collect(Collectors.toMap(Channel::getId, Channel::getName));
        Map<Integer, String> rankListMap = rankLists.stream().collect(Collectors.toMap(RankList::getId, RankList::getName));


        LambdaQueryWrapper<Music> queryWrapper =
                new LambdaQueryWrapper<>(Music.class)
                        .setEntityClass(Music.class);

        if ("name".equals(type)){
            queryWrapper.like(Music::getName, mes);
        }

        if ("singer".equals(type)){
            queryWrapper.like(Music::getSinger, mes);
        }

        if ("rank".equals(type)){
            queryWrapper.like(Music::getRank, mes);
        }

        IPage<Music> result = Db.page(new Page<>(pageNo, 10),queryWrapper);

//        List<Music> list = Db.list(queryWrapper);

        List<MusicVo> musicVos = new ArrayList<>();

        result.getRecords().stream().forEach(music -> {
            int rankListId = music.getRankListId();
            int channelId = music.getChannelId();
            MusicVo musicVo = BeanUtil.copyProperties(music, MusicVo.class);
            musicVo.setRankListName(rankListMap.get(rankListId));
            musicVo.setChannelName(channelMap.get(channelId));
            musicVos.add(musicVo);
        });


        return new MusicPageVo(musicVos, result.getTotal(), result.getPages(), pageNo);
    }


}
